package com.hundredhealth.rose.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import com.google.gson.reflect.TypeToken;
import com.hundredhealth.rose.R;
import com.hundredhealth.rose.adapter.BasePullUpRecyclerAdapter;
import com.hundredhealth.rose.adapter.RecyclerHolder;
import com.hundredhealth.rose.api.Api;
import com.hundredhealth.rose.model.HealthActivity;
import com.kymjs.rxvolley.RxVolley;

import java.lang.reflect.Type;
import java.util.ArrayList;

import rx.Observable;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.functions.Func1;
import rx.schedulers.Schedulers;

public class CurrentActivityListActivity extends MainListActivity<HealthActivity> {

    private Subscription cacheSubscript;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        cacheSubscript = Observable.just(RxVolley.getCache(Api.ACTIVITY_CURRENT_LIST))
                .filter(new Func1<byte[], Boolean>() {
                    @Override
                    public Boolean call(byte[] cache) {
                        return cache != null && cache.length != 0;
                    }
                })
                .map(new Func1<byte[], ArrayList<HealthActivity>>() {
                    @Override
                    public ArrayList<HealthActivity> call(byte[] bytes) {
                        return parserInAsync(bytes);
                    }
                })
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<ArrayList<HealthActivity>>() {
                    @Override
                    public void call(ArrayList<HealthActivity> activities) {
                        datas = activities;
                        adapter.refresh(datas);
                        viewDelegate.mEmptyLayout.dismiss();
                    }
                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {
                    }
                });
        viewDelegate.setBackgroundImage(R.drawable.bg3);
        getSupportActionBar().setDisplayShowHomeEnabled(true);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    }

    @Override
    protected BasePullUpRecyclerAdapter<HealthActivity> getAdapter() {
        return new BasePullUpRecyclerAdapter<HealthActivity>(recyclerView, datas, R.layout.item_activity) {
            @Override
            public void convert(RecyclerHolder holder, HealthActivity item, int position) {
                holder.setText(R.id.activityDateView, item.getDate());
                holder.setText(R.id.activityVendorNameView, item.getVendorName());
                holder.setText(R.id.activityLocationView, item.getLocation());
            }
        };
    }

    @Override
    protected ArrayList<HealthActivity> parserInAsync(byte[] t) {
        String jsonArray = new String(t);
        Type listType = new TypeToken<ArrayList<HealthActivity>>() {
        }.getType();
//        return new Gson().fromJson(jsonArray, listType);
        ArrayList<HealthActivity> healthActivityList = new ArrayList<>();
        HealthActivity healthActivity1 = new HealthActivity("001", 31.315225D, 120.615245D, "苏州市姑苏区慈敬阁老年公寓", "2017/06/23 08:00");
        HealthActivity healthActivity2 = new HealthActivity("002", 31.251092D, 120.742553D, "苏州工业园区普惠路财富广场肯德基", "2017/06/23 09:00");
        healthActivityList.add(healthActivity1);
        healthActivityList.add(healthActivity2);
        return healthActivityList;
    }

    @Override
    public void doRequest() {
        new RxVolley.Builder().url(Api.ACTIVITY_CURRENT_LIST)
                .httpMethod(RxVolley.Method.GET)
                .cacheTime(100)
                .callback(callBack)
                .doTask();
    }

    @Override
    public void onItemClick(View view, Object data, int position) {
        HealthActivity healthActivity = ((HealthActivity) data);
        Intent intent = new Intent(this, CustomerListActivity.class);
        Bundle bundle = new Bundle();
        bundle.putSerializable(CustomerListActivity.KEY_ACTIVITY_ID, healthActivity.getId());
        intent.putExtras(bundle);
        startActivity(intent);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (cacheSubscript != null && cacheSubscript.isUnsubscribed())
            cacheSubscript.unsubscribe();
    }
}
